package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.ServerCollect;
import com.etone.smartAudit.mapper.ServerCollectMapper;
import com.etone.smartAudit.utils.BooleanUtil;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.serverInfo.serverCollect.CpuUsedRateVO;
import com.etone.smartAudit.vo.serverInfo.serverCollect.DiskUsedVO;
import com.etone.smartAudit.vo.serverInfo.serverCollect.MemoryUsedVO;
import com.etone.smartAudit.vo.serverPath.ServerPathVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: sovy
 * @date: 2020/10/14
 * @description:
 */

@Service
public class ServerCollectService extends ServiceImpl<ServerCollectMapper, ServerCollect> {

    /**
     * 清理数据
     *
     * @param startTime
     * @param endTime
     */
    public int cleanServerInfo(Date startTime, Date endTime) {
        return baseMapper.cleanServerInfo(startTime, endTime);
    }

    /**
     * cpu使用率
     *
     * @param id
     * @param startTime
     * @param endTime
     * @return
     */
    public List<CpuUsedRateVO> cpuUsedRateRecord(Integer id, Date startTime, Date endTime) {
        return baseMapper.cpuUsedRateRecord(id, startTime, endTime);
    }

    /**
     * 内存使用情况
     *
     * @param id
     * @param startTime
     * @param endTime
     * @return
     */
    public List<MemoryUsedVO> memoryUsedRecord(Integer id, Date startTime, Date endTime) {
        return baseMapper.memoryUsedRecord(id, startTime, endTime);
    }

    /**00
     * 磁盘使用情况
     *
     * @param id
     * @param startTime
     * @param endTime
     * @return
     */
    public List<DiskUsedVO> diskUsedRecord(Integer id, Date startTime, Date endTime) {
        return baseMapper.diskUsedRecord(id, startTime, endTime);
    }

    /**
     * 服务器项目资源
     *
     * @param id
     * @param page
     * @param pageSize
     * @return
     */
    public PagerVO<ServerPathVO> listServerItems(Integer id, int page, int pageSize) {
        Page<ServerPathVO> serverPathVOPage = PageHelper.startPage(page, pageSize).doSelectPage(() -> baseMapper.listServerItems(id));
        return PagerVO.of(serverPathVOPage);
    }

    /**
     * 查看所有服务器项目资源
     *
     * @param admin
     * @param all
     * @return
     */
    public List<ServerPathVO> listAllServerItem(Admin admin, Boolean all) {
        return baseMapper.listAllServerItem(BooleanUtil.isTrue(all) || admin.isAdmin() ? null : admin.getId());
    }
}
